package com.eliza.cmps.activity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.eliza.cmps.R
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking


class ProgressBarActivity : AppCompatActivity() {
    private var currentProgress = 0 //当前进度值
    private var maxProgress = 0 //最大进度值
    lateinit var progressBar: ProgressBar

    private var handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0 -> progressBar.progress = currentProgress
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_progressbar)
        progressBar = findViewById<ProgressBar>(R.id.progressBar_pb);
        maxProgress = progressBar.max;
    }

    override fun onStart() {
        super.onStart()
        var sd = runBlocking {
            launch {
                delay(1000)
                currentProgress += 10;
                if (currentProgress > maxProgress) return@launch
                handler.sendEmptyMessage(0);
            }
        }
        sd.start()

    }

}

